package elasticsearch;



import org.elasticsearch.action.bulk.BulkItemResponse;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequestBuilder;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequestBuilder;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


public class ElasticSearchTest {
    /**
     * 插入文档
     */
    @Test
    public void testAddDocument(){
        //获取客户端对象
        TransportClient client = ESClientUtil.getClient();
        //创建索引
        IndexRequestBuilder indexRequestBuilder = client.prepareIndex("shes", "hers", "1");
        Map<String,Object> data = new HashMap<>();
        data.put("id",1);
        data.put("username","谌媾觏");
        data.put("age",250);
        //获取结果
        IndexResponse indexResponse = indexRequestBuilder.setSource(data).get();
        System.out.println(indexResponse);
        client.close();
    }
    /**
     * 根绝id 查找文档
     */
    @Test
    public void  testFindDocument(){
        //获取客户端对象
        TransportClient client = ESClientUtil.getClient();
        //获取索引
        GetRequestBuilder getRequestBuilder = client.prepareGet("shes", "hers", "1");
        //获取索引中的源数据
        Map<String, Object> source = getRequestBuilder.get().getSource();
        //打印输出 获取到的数据
        System.out.println(source);
    }

        @Test
        public void batchAddDocument(){
        //获取客户端对象
        TransportClient client = ESClientUtil.getClient();
            //通过es的客户端对象  获取BulkRequestBuilder对象
            //BulkRequestBuilder是用来批处理的对象  把需要操作的对象通过add方法  加进去
            //BulkRequestBuilder调用get方法   完成es的操作
            BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();

            HashMap<String, Object> map1 = new HashMap<>();
            map1.put("name","大脑壳");
            map1.put("age","250");
            IndexRequestBuilder indexRequestBuilder1 = client.prepareIndex("shes", "hers", "2").setSource(map1);
            bulkRequestBuilder.add(indexRequestBuilder1);

            HashMap<String, Object> map2 = new HashMap<>();
            map1.put("name","脑壳大");
            map1.put("age","250");
            IndexRequestBuilder indexRequestBuilder2 = client.prepareIndex("shes", "hers", "3").setSource(map2);
            bulkRequestBuilder.add(indexRequestBuilder2);

            //调用get方法 完成操作
            BulkResponse bulkItemResponses = bulkRequestBuilder.get();
            Iterator<BulkItemResponse> iterator = bulkItemResponses.iterator();
            while (iterator.hasNext()){
                System.out.println(iterator.next().getResponse());
            }
    }

    /**
     * 查employee  名字带duo   年龄大于5,小于等于20   性别=0
     * city = tokyo
     * 分页     根据年龄  降序    查name和age  city
     */
    @Test
    public void testFindByMany() {

        //建立es的连接  获得客户端对象
        TransportClient client = ESClientUtil.getClient();



        //BoolQueryBuilder 就是组合查询存放   dsl查询对象和dsl过滤对象
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();



        //通过boolQueryBuilder拿到了过滤对象
        List<QueryBuilder> filter = boolQueryBuilder.filter();

        //往过滤对象中添加过滤条件
        filter.add(QueryBuilders.rangeQuery("age").gt(1).lte(20));
        filter.add(QueryBuilders.termQuery("sex",0));
        filter.add(QueryBuilders.termQuery("city","tianshangrenjian"));



        //通过boolQueryBuilder拿到了查询对象
        List<QueryBuilder> must = boolQueryBuilder.must();
        //往过滤对象中添加查询条件
        must.add(QueryBuilders.matchQuery("name", "谌"));


        //通过客户端拿到searchRequestBuilder
        //searchRequestBuilder 综合查询最外层的查询的对象
        SearchRequestBuilder searchRequestBuilder = client.prepareSearch("ccg");
        searchRequestBuilder.setFrom(0)
                .setSize(20)
                .addSort("age", SortOrder.DESC)
                .setQuery(boolQueryBuilder);


        System.out.println(searchRequestBuilder.get());


    }

    //删除文档
    @Test
    public void testDelete(){
        //获取客户端对象
        TransportClient client = ESClientUtil.getClient();

        DeleteRequestBuilder deleteRequestBuilder = client.prepareDelete("shopping", "user", "1");
        DeleteResponse deleteResponse = deleteRequestBuilder.get();

        System.out.println(deleteResponse);
        client.close();
    }

}
